package com.woniu.cms.operation.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.operation.core.entity.OperationBasicinfo;
import com.woniu.cms.operation.core.entity.OperationQualitycheck;
import com.woniu.cms.operation.core.service.IOperationBasicinfoService;
import com.woniu.cms.operation.core.vo.OperationBasicinfoVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 项目基本信息表 前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-24
 */
@RestController
@RequestMapping("/operation-basicinfo")
public class OperationBasicinfoController {




    @Autowired
    private IOperationBasicinfoService  iOperationBasicinfoService;


    @ApiOperation("根据id删除項目基本信息数据")
    @DeleteMapping("/{id}")
    public Result deleteClientCompetitor(@PathVariable Integer id){

        boolean b = iOperationBasicinfoService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除項目基本信息表")
    @DeleteMapping
    public Result deleteClientCompetitor(@RequestBody List<Integer> ids){
        //批量删除
        boolean b = iOperationBasicinfoService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }



    @ApiOperation("修改項目基本信息表")
    @PutMapping
    public Result updateClientCompetitor(@RequestBody OperationBasicinfo  operationBasicinfo){

        QueryWrapper<OperationBasicinfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_name",operationBasicinfo.getProjectName());

        boolean update = iOperationBasicinfoService.update(operationBasicinfo, queryWrapper);

        return update ? Result.ok() :Result.error();
    }

    @ApiOperation("保存項目基本信息表")
    @PostMapping("savecc")
    public Result saveClientCompetitor(@RequestBody OperationBasicinfo  operationBasicinfo){

        boolean save = iOperationBasicinfoService.save(operationBasicinfo);
        return save ? Result.ok() :Result.error();
    }
    


    @ApiOperation(value = "通过項目基本信息表列表Id查找項目基本信息对象")
    @GetMapping("{id}")
    public  Result findById(@PathVariable   Integer id) {

        OperationBasicinfo operationBasicinfo = iOperationBasicinfoService.getById(id);

        return Result.common(operationBasicinfo);
    }

    @ApiOperation(value = "通过項目基本信息表中的项目名称查找項目基本信息表的详细信息")
    @GetMapping("isname")
    public  Result findByName(String projectName) {

        System.out.println("projectName = " + projectName);

        QueryWrapper<OperationBasicinfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_name",projectName);

        OperationBasicinfo operationBasicinfo = iOperationBasicinfoService.getOne(queryWrapper);

        return  Result.common(operationBasicinfo);

    }

    @ApiOperation(value = "通过項目基本信息表中的项目名称查找項目基本信息表的详细信息")
    @GetMapping("isQuailityCheck")
    public OperationQualitycheck   findQuailityCheck(String projectName) {

        System.out.println("projectName = " + projectName);

        QueryWrapper<OperationBasicinfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_name",projectName);

        OperationBasicinfo operationBasicinfo = iOperationBasicinfoService.getOne(queryWrapper);



        OperationQualitycheck operationQualitycheck = new OperationQualitycheck();

        operationQualitycheck.setProjectName(operationBasicinfo.getProjectName());
        operationQualitycheck.setProjectId(operationBasicinfo.getProjectId());

        return  operationQualitycheck;

    }
    

    @GetMapping("page")
    @ApiOperation(value = "項目基本信息表分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageOperationBasicinfo(Integer currentPage, Integer pageSize){

        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);


        IPage<OperationBasicinfo> operationBasicinfoIPage = new Page(currentPage,pageSize);

        IPage<OperationBasicinfo> page = iOperationBasicinfoService.page(operationBasicinfoIPage);

        System.out.println("page = " + page);

        List<OperationBasicinfo> records = page.getRecords();

        System.out.println("records = " + records);

        long total = page.getTotal();

        System.out.println("total = " + total);

        OperationBasicinfoVo operationBasicinfoVo = new OperationBasicinfoVo();


        List<OperationBasicinfoVo> operationBasicinfoVos = CommonBeanUtils.copyListProperties(records, OperationBasicinfoVo.class);


        Map<String,Object> map = new HashMap<>();
        map.put("data",operationBasicinfoVos );
        map.put("total",total);

        return Result.common(map);


    }


   /* private List<OperationBasicinfoVo>  copyListProperties(List<OperationBasicinfo>  records){

        List<OperationBasicinfoVo> operationBasicinfoVos  = MyBeanUtils.copyListProperties(records, OperationBasicinfoVo::new);

        return operationBasicinfoVos;

    }*/




}

